package com.tencent.compose

import appDatabase.GithubRepo
import appDatabase.GithubRepoDao
import appDatabase.GithubRepoEntity
import com.tencent.compose.sample.CloudImage
import com.tencent.compose.sample.CloudImageEntity
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

var g_fid: Int = 0;
class ListRepository(private val dao: GithubRepoDao) {

    public lateinit var datas: Flow<List<CloudImage>>


//    fun getCloudImageRepos(): Flow<List<CloudImageEntity>> {
//        return flow {
//            val count = dao.count()
//            if (count > 0) {
//                println("msyyyyyy From DB $count")
//                emit(dao.getAll())
//            } else {
//                println("msyyyyyy From APi")
////                delay(1000) // api simulation
//                var lists: List<CloudImageEntity> = emptyList();
//                for (i in 0..200) {
//                    lists =
//                        lists.plus(
//                            CloudImageEntity(
//                                fid = ++g_fid,
//                                fromFileDiff = false,
//                                category = 0,
//                                country = "cloudImage.country",
//                                province = "cloudImage.province",
//                                city = "cloudImage.city",
//                                district = "cloudImage.district",
//                                street = "cloudImage.street",
//                                businessDistrict = "cloudImage.businessDistrict",
//                                serverMTime = 0.0,
//                                takenTime = 0.0,
//                                hasVector = false,
//                                fileMd5 = "https://img.remit.ee/api/file/BQACAgUAAyEGAASHRsPbAAECFzRoyTruC1XQ30xMYfykc3Sb--Y-fgACTDoAAvOKSVZA5R5pkmTR1zYE.png",
//                                fileSize = 0,
//                                collected = false,
//                                fromType = 0,
//                                isDeleted = false,
//                                extentInt8 = 0,
//                                serverCTime = 0.0,
//                                localCTime = 0.0,
//                                localMTime = 0.0,
//                                year = 0,
//                                month = 0,
//                                day =  0,
//                                formattedAddress = "cloudImage.formattedAddress",
//                                duration = 0.0,
//                                showInAlbum = false,
//                                serverPath = "",
//                                fileName = "",
//                                orientation = 0,
//                                resolution = "",
//                                latitude = 0.0,
//                                longitude = 0.0,
//                            )
//                        )
//                }
//                println("msyyyyyy dao.insert")
//                dao.insert(lists)
//                emit(lists)
//            }
//        }
//    }

    fun getKotlinTopRepos(): Flow<List<GithubRepo>> {
        return flow {
            val count = dao.count()
            if (count > 0) {
                println("msyyyyy From DB $count")
                emit(dao.getAll().map {
                    GithubRepo(it.id, it.name, it.stars, it.description)
                })
            } else {
                println("msyyyyy From APi")
//                delay(1000) // api simulation
                val results = getRepos()
                dao.insert(results.map {
                    GithubRepoEntity(it.id, it.name, it.stars, it.description)
                })
                emit(results)
            }
        }
    }

    private fun getRepos(): List<GithubRepo> {

        var lists: List<GithubRepo> = emptyList();
        for (i in 0..200) {
            println("msyyyyy plus g_fid" + g_fid)
            lists =
                lists.plus( GithubRepo(
                    ++g_fid,
                    "JetBrains/kotlin",
                    "48.3k",
                    "https://img.remit.ee/api/file/BQACAgUAAyEGAASHRsPbAAECFzRoyTruC1XQ30xMYfykc3Sb--Y-fgACTDoAAvOKSVZA5R5pkmTR1zYE.png"
                ))
        }
        return lists
    }


}